package com.zoe.onelink.es.util;

import java.text.MessageFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-10-13
 */
public class EsIndexGenerateUtil {

    /**
     * 根据日期生成索引名称
     * 例如 输入:[2022-10-13,2022-10-15] 输出:[2022-10-13,2022-10-14,2022-10-15]
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return 索引名称列表
     */
    public static List<String> generateDateIndexNamesOfDays(LocalDate startDate, LocalDate endDate) {
        return generateDateIndexNamesOfDays(startDate, endDate, "yyyy-MM-dd");
    }

    /**
     * 根据日期生成索引名称
     * 例如 输入:[2022-10-13,2022-10-15] 输出:[2022-10-13,2022-10-14,2022-10-15]
     *
     * @param startDate  开始日期
     * @param endDate    结束日期
     * @param dateFormat 日期格式
     * @return 索引名称列表
     */
    public static List<String> generateDateIndexNamesOfDays(LocalDate startDate, LocalDate endDate, String dateFormat) {
        if (startDate == null || endDate == null) {
            throw new IllegalArgumentException("startDate or endDate not null");
        }
        if (startDate.isAfter(endDate)) {
            String msg = MessageFormat.format("[startDate:{0}] must be earlier than [endDate:{1}]", startDate, endDate);
            throw new IllegalArgumentException(msg);
        }
        int days = Period.between(startDate, endDate).getDays();
        List<String> indexNames = new ArrayList<>(days);
        for (int i = 0; i < days; i++) {
            String indexName = startDate.format(DateTimeFormatter.ofPattern(dateFormat));
            indexNames.add(indexName);
            startDate = startDate.plusDays(1);
        }
        return indexNames;
    }


}
